package com.burgeon.backend.Controller;

import com.burgeon.backend.Util.RedisUtil;
import com.burgeon.backend.entity.Account;
import com.burgeon.backend.entity.Info;
import com.burgeon.backend.service.BackendService;
import com.burgeon.backend.service.ex.ErrorDataException;
import com.burgeon.backend.stsutil.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Random;

@RestController
@RequestMapping("backend")
@CrossOrigin
public class BackendController extends BaseController {

    @Autowired
    private BackendService backendService;
    @Autowired
    private RedisUtil redisUtil;

    /**
     * 生成验证码
     * @param
     * @return
     */
    @RequestMapping("code")
    public ResponseResult<String> verificationCode(String key){

        Random random = new Random();
        String ran1 = String.valueOf(random.nextInt(90000)+10000);
        redisUtil.set("verification_"+key,ran1,60*10L);
        System.err.println(key);
        System.err.println(ran1);
        return new ResponseResult<>(SUCCESS,ran1);
    }

    @RequestMapping("bosxe")
    public ResponseResult<String> BosOrXe(String account){

        String data = backendService.BosOrXe(account);
        return new ResponseResult<>(SUCCESS,data);
    }

    @RequestMapping("exit")
    public void Exit(HttpSession session){
        session.removeAttribute("account");
    }

    @RequestMapping("login")
    public ResponseResult<Account> LoginSystem(String username,String password,String key,String verification){

        String code = redisUtil.get("verification_"+key);
        System.err.println(code);
        System.err.println(verification);
        if (code==null||!code.equals(verification)||code.equals("")){
            throw new ErrorDataException("验证码错误");
        }

        Account data = backendService.Login(username,password);

        return new ResponseResult<Account>(SUCCESS,data);
    }

    @RequestMapping("bosconfig")
    public ResponseResult<Info> BosConfig(Info info,String acc){
        //String account = session.getAttribute("account").toString();
        Info data = backendService.InsertInfo(info,acc);

        return new ResponseResult<Info>(SUCCESS,data);
    }

    @RequestMapping("bosconfigdata")
    public ResponseResult<Info> BosConfigData(String account){
        //String account = session.getAttribute("account").toString();
        Info data = backendService.GetInfoData(account);

        return new ResponseResult<Info>(SUCCESS,data);
    }
    @RequestMapping("changepassword")
    public ResponseResult<Void> ChangePassword(String oldpassword,String newpassword,String account){

        backendService.ModPassword(account,oldpassword,newpassword);

        return new ResponseResult<>(SUCCESS);
    }
}
